package cn.yyh.servicePet.controller;


import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.utils.GetTokenUtil;
import cn.yyh.commons.utils.PageInfo;
import cn.yyh.commons.utils.R;
import cn.yyh.servicePet.domain.Pet;
import cn.yyh.servicePet.query.PetQuery;
import cn.yyh.servicePet.service.IPetService;
import cn.yyh.servicePet.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: PetController
 * @Package: cn.yyh.pet.controller
 * @date 2021/7/6
 */
@RestController
@RequestMapping("/servicePet/pet")
public class PetController {
    @Autowired
    private IPetService petService;

    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") Long id) {
        try {
            Pet byId = petService.getById(id);
            return R.oK().data("pet",byId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        try {
            petService.deleteBatch(ids);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("update")
    public R update(@RequestBody Pet pet) {
        try {
            petService.update(pet);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("petOrder")
    public R updateByOrder(@RequestBody Pet pet) {
        try {
            petService.updateByOrder(pet);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody PetQuery query, HttpServletRequest request){
        try {
            PageInfo<Pet> info = petService.pageQuery(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(), GetTokenUtil.getToken(request));
            return R.oK().data("rows", info.getRows()).data("total",info.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PutMapping("/onsale")
    public R onSalePet(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PutMapping("/offsale")
    public R offSalePet(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("{id}")
    public R getPetDetailsById(@PathVariable Long id) {
        try {
            Pet pet = petService.getPetDetailsById(id);
            return R.oK().data("pet",pet);
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @GetMapping("adopt/{id}")
    public R adoptPet(@PathVariable Long id,HttpServletRequest request) {
        try {
            petService.adopt(id,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }
}
